package expression;

import java.util.LinkedList;

public class FnArithAdd extends Fn implements FnNumber, FnString {

	boolean concat = false;

	@Override
	protected boolean isAcceptableChild(Fn fn) {
		return (fn instanceof FnNumber || (fn instanceof FnString && (concat = true)));
	}
	
	@Override
	protected boolean isAcceptableParam(Object o) {
		return (o instanceof Number || (o instanceof String && (concat = true)));
	}

	@Override
	public int getNParam() {
		return 2;
	}

	@Override
	protected int precedence() {
		return 5;
	}

	@Override
	protected Object value(LinkedList<Object> param) {
		if (concat) {
			String out = "";
			for (Object operand : param)
				out += operand;
			return out;
		} else {
			Double out = new Double(0);
			for (Object operand : param)
				if (operand instanceof Integer)
					out += (Integer) operand;
				else if (operand instanceof Float)
					out += (Float) operand;
				else if (operand instanceof Double)
					out += (Double) operand;
				else if(operand instanceof String && (concat = true))
					return value(param);
			return out;
		}
	}

	@Override
	public String symbol() {
		return "+";
	}

	@Override
	protected String[] notation() {
		return new String[]
		{ "", " + ", "" };
	}

}
